import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
    RECEIVE_USER_INFO,
    RESET_USER_INFO,
    RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART
} from './mutation-types'

import {
    reqAddress,
    reqCategory,
    reqShops,
    reqUserInfo,
    reqLogout,
    reqShopGoods,
    reqShopRatings,
    reqShopInfo
} from '../api/index.js'
export default {
    // 3个方法
    // 方法一 触发 address
    // 方法二 触发 categorys
    // 方法三 触发 shops
    async getAddress({ commit, state }) {
        // latitude: 40.10038, // 纬 度        
        let { latitude, longitude } = state
        console.log(latitude);
        console.log(longitude);

        // commit(RECEIVE_ADDRESS, data)
        let params = latitude + ',' + longitude
        console.log(params);

        let result = await reqAddress(params)
        console.log(result);

        commit(RECEIVE_ADDRESS, { address: result.data })
    },
    // 获取产品分类
    async getCategorys({ commit }) {
        let result = await reqCategory()
        commit(RECEIVE_CATEGORYS, { categorys: result.data })
    },
    // http://localhost:3000/shops?latitude=40.10038&longitude=116.36867
    //获取商家数组
    async getShops({ commit, state }) {
        let { latitude, longitude } = state
        let result = await reqShops({ latitude, longitude })
        commit(RECEIVE_SHOPS, { shops: result.data })
    },
    recordUser({ commit }, user) {
        commit(RECEIVE_USER_INFO, { userInfo: user })
    },
    // 异步获取用户会话信息
    async getUserInfo({ commit }) {
        let result = await reqUserInfo()
        if (result.code == 0) {
            let user = result.data
            commit(RECEIVE_USER_INFO, { userInfo: user })
        }
    },
    // 异步退出登录
    async getLogout({ commit }) {
        let result = await reqLogout()
        if (result.code == 0) {
            commit(RESET_USER_INFO)
        }
    },
    // 异步获取商家商品信息
    async getShopGoods({ commit }, callback) {
        let result = await reqShopGoods()
        if (result.code == 0) {
            let goods = result.data
            commit(RECEIVE_GOODS, { goods })
            callback & callback()
        }
    },
    // 异步获取商家商品个人信息
    async getShopInfo({ commit }) {
        let result = await reqShopInfo()
        if (result.code == 0) {
            let info = result.data
            commit(RECEIVE_INFO, { info })
        }
    },
    // 异步获取商家评价
    async getShopRatings({ commit }) {
        let result = await reqShopRatings()
        if (result.code == 0) {
            let ratings = result.data
            commit(RECEIVE_RATINGS, { ratings })
        }
    },
    // 更新购物车数量方法
    updateFoodCount({ commit }, { isflag, food }) {
        if (isflag) { //表示添加
            commit(INCREMENT_FOOD_COUNT, { food })
        } else { //表示减少
            commit(DECREMENT_FOOD_COUNT, { food })
        }
    },
    // 清空购物车
    clearCart({ commit }) {
        commit(CLEAR_CART)
    }
}